Skip to content

Preserve exact comparisons in fast permutation entropy - #53

Merged
raphaelvallat merged 3 commits into
raphaelvallat:masterfrom
antonsoo:fix/permutation-entropy-ordinal-comparisons
Oct 17, 2026
Merged

raphaelvallat merged 3 commits into
raphaelvallat:masterfrom
antonsoo:fix/permutation-entropy-ordinal-comparisons

Conversation

@antonsoo

@antonsoo antonsoo commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

The fast permutation-entropy path casts values to float64 and adds jitter to break ties. This can erase large integer differences or reorder close floating-point values. Direct comparisons on the original values with positional tie breaking match stable sorting.

The review follow-up explicitly returns NaN for an affected order 3 or 4 epoch before it can produce invalid lookup keys or contaminate another epoch's histogram. Valid epochs are processed separately; self-comparison detects NaN while preserving integer and object-dtype comparisons. Update the bit-key comments to match the comparisons.

All 32 project tests and repository Ruff checks pass. The 2,560 original recorded/transformed feature checks retain their exact result records and agree with the independent stable-sort/counting oracle. In 96 additional recorded batches with explicitly inserted missing samples, invalid epochs now return NaN and all valid neighbours agree with the reference, correcting eight contaminated neighbour values. The two independent PRs also compose locally, with 34 tests and both original workloads passing.

@raphaelvallat raphaelvallat left a comment •

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @antonsoo for opening this PR. See AI review comments below:

The tie-handling change looks correct: <= breaks ties by position, which matches stable argsort, and the lookup tables still line up because <= and < agree whenever there are no ties. The main concern is NaN input. Removing the jitter also removed the behavior where NaN spread to the whole result, and nothing replaces it. The first two issues below were reproduced on this branch.

NaN regressions

  1. Order 4 crashes (inline, line 115). With a NaN, the comparisons produce bit patterns that match no ordering, so _PE4_LOOKUP returns -1 and bincount rejects it. perm_entropy(np.array([1, np.nan, 2, 3, 5, 4, 1, 2.]), order=4) raises ValueError: 'list' argument must have no negative elements. On master it returned -0.0.
  2. With 2D order-4 input, a NaN in one epoch changes another epoch's result (inline, line 115). A -1 key plus offset k*24 lands in the last bin of epoch k-1. In one example, epoch 0 gave 4.0926 instead of 3.9698, and its probabilities sum to more than 1.
  3. Order 3 gives a wrong answer with no warning (inline, line 101). The keys 0b010 and 0b101 map to 0 in _PE3_LOOKUP and _PE3_REMAP, so they are counted as pattern 0. For example, [1, nan, 2, 3, 5, 4, 1, 2] returns 1.918.
  4. Suggested fix (inline, line 91): check for NaN up front, e.g. np.isnan(x).any() for float input, and either raise or return NaN before any keys reach the lookup tables.

Other
5. No NaN or non-finite input tests (inline, tests/test_entropy.py). The suite passes despite the issues above.
6. Out-of-date comments (inline, line 38; also line 54). The comments describing each key bit still show strict < while the code now uses <=. Anyone rebuilding the tables from these comments would bring back the tie mismatch.

Comment thread src/antropy/entropy.py Resolved
Comment thread src/antropy/entropy.py Resolved
Comment thread src/antropy/entropy.py Resolved
Comment thread tests/test_entropy.py Resolved
Comment thread src/antropy/entropy.py Resolved
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (dfbe688) to head (595f0d5).

Additional details and impacted files
@@            Coverage Diff            @@
##            master       #53   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            4         4           
  Lines          370       374    +4     
  Branches        62        63    +1     
=========================================
+ Hits           370       374    +4     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@raphaelvallat raphaelvallat left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution @antonsoo ! Merging now and I'll release a new version of Antropy

@raphaelvallat
raphaelvallat merged commit 5d403ce into raphaelvallat:master Oct 17, 2026
30 of 34 checks passed
@raphaelvallat raphaelvallat mentioned this pull request Oct 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants